package com.cysion.study.ui

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import com.cysion.study.ktbox.Box
import com.cysion.study.ktbox.base.BaseActivity
import com.cysion.study.ktbox.base.ITEM_CLICK
import com.cysion.study.ktbox.net.BaseResponseRx
import com.cysion.study.ktbox.net.ErrorHandler
import com.cysion.study.ktbox.utils.CLog
import com.cysion.study.ktbox.utils.whiteTextTheme
import com.cysion.other.color
import com.cysion.targetfun._subscribe
import com.cysion.study.uibox.bar.TopBar
import com.cysion.study.uibox.dialog.Alert
import com.cysion.study.R
import com.cysion.study.adapter.BankListAdapter
import com.cysion.study.entity.BankListEntity
import com.cysion.study.helper.UserCache
import com.cysion.study.net.ApiClient
import com.cysion.study.widget.ShowToast
import com.scwang.smartrefresh.layout.constant.RefreshState
import kotlinx.android.synthetic.main.activity_bank_list.*

class BankListActivity : BaseActivity() {
    override fun getLayoutId(): Int = R.layout.activity_bank_list


    private lateinit var proAdapter: BankListAdapter
    private val noticeList: MutableList<BankListEntity> = mutableListOf()
    private var curPage = 1
    override fun initView() {
        whiteTextTheme(color(R.color.main_cl))
        topbar.apply {
            setTitle("选择银行")
            setOnTopBarClickListener { obj, pos ->
                if (pos == TopBar.Pos.LEFT) {
                    finish()
                }
            }
        }
        initRefreshLayout()
        initRecyclerView()
    }

    //加载数据
    override fun initData() {
        super.initData()
        smartLayout.autoRefresh()
    }

    //    初始化刷新控件
    private fun initRefreshLayout() {
        smartLayout.setOnRefreshListener {
            curPage = 1
            getDataList(curPage)
            smartLayout.setEnableLoadMore(true)

        }
        smartLayout.setOnLoadMoreListener {

            getDataList(curPage)
        }
    }

    private fun getDataList(page: Int) {
        Alert.loading(self)
        ApiClient.api.getBanks(UserCache.token).compose(BaseResponseRx.validateToMain())
            ._subscribe {
                _onNext {
                    Alert.close()
                    stopRefresh()
                    CLog.i("###="+it.size)
                   /* if (curPage == 1) {
                        noticeList.clear()
                        if (noticeList.size == 0) {
                            multiView.showEmpty()
                        }
                    }
                    if (it.size <= 0) {
                    } else {
                        curPage++
                        noticeList.addAll(it)
                        proAdapter.notifyDataSetChanged()
                        multiView.showContent()
                    }*/
                }
                _onError {
                    Alert.close()
                    stopRefresh()
                    ShowToast(ErrorHandler.handle(it).errorMsg)
                }
            }

    }

    private fun stopRefresh() {
        if (smartLayout.state == RefreshState.Refreshing) {
            smartLayout.finishRefresh()
        } else if (smartLayout.state == RefreshState.Loading) {
            smartLayout.finishLoadMore(100)
        }
    }

    //初始化列表
    private fun initRecyclerView() {
        rv_list.isNestedScrollingEnabled = false
        proAdapter = BankListAdapter(noticeList, Box.context)
        rv_list.adapter = proAdapter
        rv_list.layoutManager = LinearLayoutManager(Box.context)

        proAdapter.setOnTypeClickListener { obj, position, flag ->
            if (flag == ITEM_CLICK) {
                var bundle = Bundle()
//                bundle.putString("name",obj.value.toString())
                var intent = Intent()
                intent.putExtra("key", bundle)

                setResult(Activity.RESULT_OK, intent)
                finish()
            }
        }
    }

    override fun closeMvp() {
    }

}
